FROM golang:1.18 as build

ENV GOPROXY=https://goproxy.cn,direct
ENV GOPRIVATE=chainmaker.org
WORKDIR /smarteditor
COPY . .
RUN make build 
WORKDIR /smarteditor-build 
RUN cp -r /smarteditor/smartEditor . && \
    cp -r /smarteditor/etc . && \
    cp -r /smarteditor/data . && \
    cp -r /smarteditor/docs . && \
    cp -r /smarteditor/static . && \
    cp -r /smarteditor/views . && \
    cp -r /smarteditor/gulpfile.js . && \
    cp -r /smarteditor/package-lock.json . && \
    cp -r /smarteditor/package.json . && \
    cp -r /smarteditor/contract-sdk-docker-go-ide . && \
    cp -r /smarteditor/contract-sdk-go . 

FROM ubuntu:22.04
RUN rm /bin/sh && ln -s /bin/bash /bin/sh
RUN apt-get update && apt-get -qq install -y  --no-install-recommends ca-certificates curl vim net-tools tree gcc golang p7zip-full p7zip-rar g++
ENV TZ "Asia/Shanghai"
RUN DEBIAN_FRONTEND=noninteractive apt-get install -y tzdata && \
    echo $TZ > /etc/timezone && \
    ln -fs /usr/share/zoneinfo/$TZ /etc/localtime && \
    dpkg-reconfigure tzdata -f noninteractive  

WORKDIR /smarteditor  

COPY --from=build ./smarteditor-build/ .  
WORKDIR /
RUN mkdir -p /root/go/src && \
    mkdir -p /root/go/bin && \
    mkdir -p /root/go/pkg && \
    go env -w GOPROXY="https://goproxy.cn,direct" && \
    go install github.com/visualfc/gotools@latest && \
    go install github.com/stamblerre/gocode@latest && \
    originGoROOT=$(go env GOROOT) && \
    mkdir -p /data/gopath230/src && \
    mkdir -p /data/gopath230/bin && \  
    mkdir -p /data/gopath230/pkg && \
    mkdir -p /data/gopath230copy/src && \
    mkdir -p /data/gopath230copy/bin && \  
    mkdir -p /data/gopath230copy/pkg && \    
    mkdir -p /data/go230 && \
    mkdir -p /data/go230copy && \
    cd ${originGoROOT} && \
    cp -Lrf * /data/go230 && \
    cp -Lrf * /data/go230copy && \
    cd /data/go230/src && \
    cp -rf /smarteditor/contract-sdk-docker-go-ide chainmaker && \
    cp /root/go/bin/gotools ../bin && \
    cp /root/go/bin/gocode ../bin && \
    rm -rf -v chainmaker/contract* && \
    rm -rf -v chainmaker/.DS_Store && \
    rm -rf -v chainmaker/test && \
    rm -rf -v chainmaker/.git* && \
    rm -rf -v chainmaker/go.mod && \
    rm -rf -v chainmaker/go.sum && \
    rm -rf -v chainmaker/pb/proto/ && \
    rm -rf -v chainmaker/pb/Makefile && \
    rm -rf -v vendor && \
    cd /data/go230copy/src && \
    cp -rf /smarteditor/contract-sdk-go chainmaker && \
    rm -rf -v chainmaker/.git* && \
    rm -rf -v chainmaker/demo/ && \
    rm -rf -v chainmaker/go.mod && \
    rm -rf -v chainmaker/go.sum && \    
    rm -rf -v chainmaker/main.go && \
    rm -rf -v chainmaker/Makefile && \
    rm -rf -v chainmaker/build.sh && \
    rm -rf -v chainmaker/pb/proto/ && \
    rm -rf -v vendor && \
    sed -i "s/chainmaker.org\\/chainmaker\\/contract-sdk-go\\/v2/chainmaker/g" $(grep contract-sdk-go -rl /data/go230copy/src/chainmaker)

EXPOSE 7080 50051 

WORKDIR /smarteditor

ENTRYPOINT [ "./smartEditor","--conf" ]
CMD [ "./etc/wide_pro.json" ]